package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.exercise.ExerciseOption;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 习题选项Dao
 * Created by wyh on 2015/2/6.
 */
@Repository
public class ExerciseOptionDao extends BaseDao {

    /**
     * 批量新增选项
     * @param options 选项集合
     * @return int[]
     *
     * @author wyh
     * @since  2015-2-5
     */
    public int[] saveExerciseOptions(long exerciseId, List<ExerciseOption> options){
        String sql = "INSERT INTO t_exercise_option(iExerciseId,sOption,tiRight) VALUES("+exerciseId+",:option,:isRight)";

        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(options.toArray());

        log.debug("批量新增选项");
        return namedParameterJdbcTemplate.batchUpdate(sql,batch);
    }

    /**
     * 获取习题选项
     * @param exerciseId
     * @return
     * @author lyj
     * @since 2015-2-9
     */
    public List<ExerciseOption> getExerciseOptions(final long exerciseId){
        String sql = "SELECT id,sOption,tiRight FROM t_exercise_option WHERE iExerciseId = :iExerciseId";

        MapSqlParameterSource param = new MapSqlParameterSource().addValue("iExerciseId", exerciseId);

        log.debug("获取习题选项");
        return namedParameterJdbcTemplate.query(sql, param, new RowMapper<ExerciseOption>() {
            @Override
            public ExerciseOption mapRow(ResultSet resultSet, int i) throws SQLException {
                ExerciseOption exerciseOption = new ExerciseOption();

                exerciseOption.setId(resultSet.getLong("id"));
                exerciseOption.setOption(resultSet.getString("sOption"));
                exerciseOption.setIsRight(resultSet.getInt("tiRight"));

                return exerciseOption;
            }
        });
    }

    /**
     * 查询不带答案的选项
     *
     * @param exerciseId 习题ID
     * @return List
     *
     * @author cx
     * @since 2015/3/25
     */
    public List<ExerciseOption> getOptionsWithoutAnswer(final long exerciseId) {
        String sql = "SELECT id,sOption FROM t_exercise_option WHERE iExerciseId=:exerciseId";

        SqlParameterSource sps = new MapSqlParameterSource().addValue("exerciseId", exerciseId);

        log.debug("查询不带答案的选项");
        return namedParameterJdbcTemplate.query(sql, sps, new RowMapper<ExerciseOption>() {
            @Override
            public ExerciseOption mapRow(ResultSet resultSet, int i) throws SQLException {
                ExerciseOption option = new ExerciseOption();

                option.setId(resultSet.getLong("id"));
                option.setOption(resultSet.getString("sOption"));

                return option;
            }
        });
    }

    /**
     * 查询正确选项的ID集合
     * 
     * @param exerciseId 习题ID
     * @return List
     * 
     * @author cx
     * @since 2015/3/26
     */
    public List<Long> getRightOptionIds(final long exerciseId) {
        String sql = "SELECT id FROM t_exercise_option WHERE iExerciseId=:exerciseId AND tiRight=1";
        
        SqlParameterSource sps = new MapSqlParameterSource().addValue("exerciseId", exerciseId);

        log.debug("查询正确选项的ID集合");
        return namedParameterJdbcTemplate.queryForList(sql, sps, Long.class);
    }

    /**
     * 查询判断题选项的正确性
     * 
     * @param exerciseId 习题ID
     * @param selectedOption 对错选项
     * @return boolean
     * 
     * @author cx
     * @since 2015/3/27
     */
    public boolean isRight(final long exerciseId, final int selectedOption) {
        String sql = "SELECT COUNT(id) FROM t_exercise_option WHERE iExerciseId=:exerciseId AND tiRight=:selectedOption";

        SqlParameterSource sps = new MapSqlParameterSource()
                .addValue("exerciseId", exerciseId)
                .addValue("selectedOption", selectedOption);

        log.debug("查询判断题选项的正确性");
        return namedParameterJdbcTemplate.queryForObject(sql, sps, Long.class) == 1;
    }
}
